#pragma once

#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <dshow.h>
#include <Windows.h>
#include <winerror.h>
#include <mmdeviceapi.h>
#include <Functiondiscoverykeys_devpkey.h>

#include <Audioclient.h>
#include <Audiopolicy.h>
#include <complex>

using namespace std;

// REFERENCE_TIME time units per second and per millisecond
#define REFTIMES_PER_SEC  10000000
#define REFTIMES_PER_MILLISEC  10000

#define EXIT_ON_ERROR(hres)  \
              if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

#define Round(d) int(floor(d+0.5))

//对复数complex取模
#define Model(complex) sqrt(norm(complex))


class CRecorder
{
private:
	vector<BYTE> m_recorder;//数据记录器
	int dataStart;//声卡开始不为零的位置
	int nDefaultCapture = 1024;//默认采样点数
	IAudioClient *m_pAudioClient;//声卡客户端
	IAudioCaptureClient *m_pCaptureClient;//捕获流客户端
	WAVEFORMATEX *m_pwfx;
public:
	BOOL play = FALSE ;//采样器开关
public:
	CRecorder();
	void init();
	void refreshRecorder();
	vector<BYTE> captureData(UINT32 nCaptureBytes);//采集下一份数据
	vector<BYTE> captureData();
	int joinData(vector<BYTE> data, int maxSize);//将新数据连接到旧数据后
	void onError(HRESULT hres);
	int getDefaultCaptureNum();

	HRESULT RecordAudioStream();

	~CRecorder();
	double coordinateTrans(int y, double maxY, int height);//坐标系转换函数
	double drawWaveform(CDC* pDC, CRect rect,vector<BYTE> output,double pc);//绘制滤波图形
	int drawFrequency(CDC* pDC, CRect rect, vector<BYTE> output);//绘制FFT变换后的频谱
	int numericalAnalysis(CDC* pDC, CRect rect);//数值分析部分显示
	int coordinateSystem(CDC* pDC, int width, int height,int x_coefficient);
	vector<BYTE> NoFiltering();
	vector<BYTE> LimiterFiltering(vector<BYTE> input);
	vector<BYTE> Mean_Filtering(vector<BYTE> input);
	vector<BYTE> Median_Filtering(vector<BYTE> input);
	vector<complex<double>> FFT(vector<complex<double>> A);
	
	int bytes2Int(BYTE higher, BYTE lower);//将两个字节拼接为一个整数
	int Islittlend();//判断主机字节序是否是小端序
};

